package com.tmq.diancan.utils;

import java.util.UUID;

/**
 * Created on 2019/8/20.
 * @author liningo
 */
public class UUIDUtil {

	/**
	 * 默认使用32字符的UUID
	 * @return
	 */
	public static String getUUID() {
		return getUUID(32);
	}

	/**
	 * 获取指定字符数的UUID
	 * @param bit
	 * @return
	 */
	public static String getUUID(int bit) {
		if (bit >= 1024 || bit <= 0) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		int beginIndex = 0;
		int endIndex = bit;
		do {
			if (endIndex-beginIndex > 32) {
				sb.append(UUID.randomUUID().toString().replaceAll("-", "").substring(beginIndex, beginIndex+32));
			}else {
				sb.append(UUID.randomUUID().toString().replaceAll("-", "").substring(beginIndex, endIndex));
			}
			endIndex -= 32;
		} while ((bit = bit-32) > 0);
		return sb.toString();
	}
	/**
	 * 获取指定字符数加时间的UUID
	 * @param bit
	 * @return
	 */
	public static String getTimeUUID(int bit) {
		String result = getUUID(bit);
		return System.currentTimeMillis()+result;
	}


}
